
$(function () {
    validateKickout();
    validateRule();
    $('.imgcode').click(function () {
        var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });
});

$.validator.setDefaults({
    submitHandler: function () {
        login();
    }
});

function login() {
    $.modal.loading($("#btnSubmit").data("loading"));
    var username = $.common.trim($("input[name='username']").val());
    var password = $.common.trim($("input[name='password']").val());
    //var validateCode = $("input[name='validateCode']").val();
    var rememberMe = $("input[name='rememberme']").is(':checked');
    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": password,
            //"validateCode": validateCode,
            "rememberMe": rememberMe
        },
        success: function (r1) {
            console.log(r1)
           
            if (r1.code == 0) {
                //获取登录用户信息判断是否需要单点跳转
                $.ajax({
                    type: "post",
                    async: false,
                    url: ctx + "system/user/ssoWyUrl",
                    data: {
                        "username": username,
                    },
                    success: function (r2) {
                 
                        if (r2.code == 0 && r2.data) {
                        	
                            $.ajax({
                                type: "get",
                                url: ctx + "logout",
                                success: function (rx) {
                                	
                                }
                            })
                            location.href = r2.data.wyUrl + 'lgBySso?operCd=' + r2.data.wyOperCd + "&operPwd=" + r2.data.wyOperPwd;

                        		
                            
                        } else {

                            location.href = ctx + 'index';
                        }

                    }
                });



            } else {
                $.modal.closeLoading();
                $('.imgcode').click();
                $(".code").val("");
                $.modal.msg(r1.msg);
            }

        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
            },
            password: {
                required: icon + "请输入您的密码",
            }
        }
    })
}

function validateKickout() {
    if (getParam("kickout") == 1) {
        layer.alert("<font color='red'>您已在别处登录，请您修改密码或重新登录</font>", {
            icon: 0,
            title: "系统提示"
        },
            function (index) {
                //关闭弹窗
                layer.close(index);
                if (top != self) {
                    top.location = self.location;
                } else {
                    var url = location.search;
                    if (url) {
                        var oldUrl = window.location.href;
                        var newUrl = oldUrl.substring(0, oldUrl.indexOf('?'));
                        self.location = newUrl;
                    }
                }
            });
    }
}

function getParam(paramName) {
    var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}

window.onload = function () {


    var loginDiv = $("#loginDiv");
    loginDiv.css('width', window.innerWidth * 0.272);
    loginDiv.css('height', window.innerHeight * 0.4768518);
    loginDiv.css('background-size', 'cover');
    loginDiv.css('border-radius', '14px');

    loginDiv.css('left', window.innerWidth * 0.5625);
    loginDiv.css('top', window.innerHeight * 0.2);

    $('#bg').css('height', window.innerHeight);
    $('#bg').css('background-image', 'url("/img/wy/bg.png")');
    $('#bg').css('background-position', 'center center');
    $('#bg').css('background-size', 'cover');


    $('.logIcon').css('width', window.innerWidth * 0.032);
    $('.logIcon').css('height', window.innerHeight * 0.047);

    $('.loginBtn').css('width', window.innerWidth * 0.184);
    $('.loginBtn').css('height', window.innerHeight * 0.047);

    $('.Login_operCd').css('width', window.innerWidth * 0.1557);
    $('.Login_operPwd').css('width', window.innerWidth * 0.1557);
    $('.Login_operCd').css('height', window.innerHeight * 0.047);
    $('.Login_operPwd').css('height', window.innerHeight * 0.047);

    $('#Login_operCd_Div').css('padding-top', $('#loginDiv').height() * 0.35);
    /* background-image: url("/img/wy/bg.png");
    background-position: center center; */

    $(".btns").on('mouseover', function () {

        var btn = $(this);
        btn.css('background', 'url("images/indexImg/arrow_2.png") no-repeat center');
    }).on('mouseout', function () {
        var btn = $(this);
        btn.css('background', 'url("images/indexImg/arrow.png") no-repeat center');
    });

    $(".registBtn").on('click', function () {

        $("#buttons .btns").fadeOut('slow');
        $("#loginDiv").fadeOut('slow', function () {
            $("#buttons .btns").fadeIn('slow');
        });

    });



}
